/**
 * BomberBoy, CPS240 Term Project - Team A
 * Andrew Osterink, David Bash, Scott Kidder
 * 	
 * 	This class that is the window and starts the main loop. The current 
 * input listeners are temporary and we should create another class to handle 
 * that. 
 */
package pkg;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.*;
import javax.swing.*;

/**
 * @author Andy
 */

@SuppressWarnings("serial")
public class Game extends JFrame implements KeyListener {

	GraphicsDevice device;
	BufferStrategy stratagy;
	Map map;
	
	public Game()
	{
		super("Bomber Boys");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(600, 600); //set default size
		this.addKeyListener(this);
		initGraphics(); 
	}
	
	private void initGraphics()
	{
		setVisible(true);//the frame must be visible to create buffer strategy
		createBufferStrategy(2);
		stratagy = getBufferStrategy();
		
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		device = ge.getDefaultScreenDevice();
		this.setIgnoreRepaint(true);
	}
	
	/**
	 * Called once per frame
	 */
	public void render()
	{
			Graphics2D g = null;
			try{
				g = (Graphics2D)stratagy.getDrawGraphics();
				g.setColor(Color.BLACK);
				g.fillRect(0, 0, getWidth(), getHeight());
			}
			finally{
				g.dispose();
			}

		if(!stratagy.contentsLost())
			stratagy.show();
		
		Toolkit.getDefaultToolkit().sync();//i have heard this is something that should be done for linux systems
	}
	
	/**
	 * Tells the window to go full screen if able
	 */
	public boolean setFullScreen(boolean full)
	{
		boolean success = false;
		setVisible(false);
		dispose(); //we must dispose resources (and recreate) resources to go full screen
		
		if(device.isFullScreenSupported() && full){
			setUndecorated(true); //remove window border
			device.setFullScreenWindow(this);
			success = true;
		}
		else {
			setUndecorated(false);
			device.setFullScreenWindow(null);
		}
		setVisible(true);
		return success;
	}
	/**
	 * Returns all display modes compatable on current system.
	 */
	public DisplayMode[] getDisplayModes()
	{
		return device.getDisplayModes();
	}
	
	/**
	 * Sets a new display mode thats compatable with system
	 * @param newMode new mode to change to. This mode must be one of the modes from getDisplayModes.
	 * @return returns true if it successfully switched display modes false otherwise.
	 */
	public boolean setDisplayMode(DisplayMode newMode)
	{
		try{
			device.setDisplayMode(newMode);
		}catch(IllegalArgumentException ex) { return false;}
		return true;
	}

	@Override
	public void keyPressed(KeyEvent e) 
	{
		if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
			this.dispose(); //if this is the last window it sould terminate the program
	}

	@Override
	public void keyReleased(KeyEvent e) {}

	@Override
	public void keyTyped(KeyEvent e) {}
	
	public static void main(String[] args)
	{
		Game game = new Game();
		game.setFullScreen(false);
		while(game.isVisible())
			game.render();	
	}
}
